#include "p2head.h"

extern int xrayroom[3];
//variables for xray room
extern List* xrayInQueue;	// sheetqueue wait for the xray(wallpocketIn)
extern List* xrayOutQueue;	// sheetqueue of done 
extern int xrayInCount;	//number of patient who is waiting for the xray
extern int xrayOutCount;	//number of patient who has finished the xray

//Lock and CVs for xray room
extern Lock* xrayLock[3];
extern Condition* xrayCondition[3];

//nurse gives sheet to xraytechnician
extern struct examSheet* xraysheet[3];	

//for interaction of nurse and patient in the xray waiting room
extern Lock xrayOutLock;		
extern Condition xrayOutC;	
extern Lock xrayInLock;
extern Condition xrayInC;
extern int testcontrol;
extern bool* patientmv;
extern bool xraymv[3];
extern Lock* roomLock[6];
extern Condition* roomCondition[6];
	
void XrayTechnician(int which){
	examSheet* mysheet;		//the sheet of the current patient
	int imagescount;
	int r;
	srand((int)time(NULL));
	
	while(true){
		
		xrayInLock.Acquire();	
		if(xrayInCount>0){		//some patient is waiting	
			mysheet=(struct examSheet*)xrayInQueue->Remove();
			mysheet->xrayroom=which;
			xrayInC.Signal(&xrayInLock);
			xrayInCount--;
			//get a sheet from the wall pocket		
			
			xrayLock[which]->Acquire();
			xrayInLock.Release();
		}
		else{
			xrayroom[which]=1;		//I'm free
			xrayLock[which]->Acquire();
			xrayInLock.Release();
			
			xrayCondition[which]->Wait(xrayLock[which]);//wait for a nurse
			//getsheet from the nurse
			mysheet=xraysheet[which];
			xrayCondition[which]->Signal(xrayLock[which]);	//inform the nurse that i get the sheet

			while(true){
				if(patientmv[mysheet->name]==false)
					currentThread->Yield();
				else
					break;
			}
			
			roomLock[mysheet->exRoomNum]->Acquire(); 
			roomCondition[mysheet->exRoomNum]->Signal(roomLock[mysheet->exRoomNum]);
			roomLock[mysheet->exRoomNum]->Release(); 
			
/*
			xrayInLock.Acquire();
			xrayInC.Signal(&xrayInLock); 	//wake up the patient brought by the nurse
			xrayInCount--;		
			xrayInLock.Release();
*/		}	

	//interaction with the patient
	
	if(testcontrol==7)
		if(mysheet->patientType==1)
			printf("Xray technician [%d] asks Adult Patient [%d] to get on the table.\n",which,mysheet->name);
		else
			printf("Xray technician [%d] asks Child Patient [%d] to get on the table.\n",which,mysheet->name);

	xrayCondition[which]->Wait(xrayLock[which]);	//wait for the response of patient

	for(imagescount=1;imagescount<=mysheet->xray;imagescount++){	
	//take a xray photo
		if(imagescount!=1)		
			if(testcontrol==7)
				if(mysheet->patientType==1)
					printf("Xray Technician [%d] asks Adult Patient [%d] to move.\n",which,mysheet->name);
				else
					printf("Xray Technician [%d] asks Child Patient [%d] to move.\n",which,mysheet->name);

				xrayCondition[which]->Signal(xrayLock[which]);
				xrayCondition[which]->Wait(xrayLock[which]);
	
				r=rand()%3;
				
			if(r==0){
				strcpy(mysheet->xrayresult[imagescount-1],"Nothing");
			}else if(r==1){
				strcpy(mysheet->xrayresult[imagescount-1],"Break");
			}else{
				strcpy(mysheet->xrayresult[imagescount-1],"compound fracture");
			}				

			if(testcontrol==7)	
				if(mysheet->patientType==1)
					printf("Xray Technician [%d] takes an Xray Image of Adult Patient [%d].\n",which,mysheet->name);
				else
					printf("Xray Technician [%d] takes an Xray Image of Child Patient [%d].\n",which,mysheet->name);
			
			if(testcontrol==7)
				if(mysheet->patientType==1)
					printf("Xray Technician [%d] records [%s] on Adult Patient [%d]'s examination sheet.\n",which,mysheet->xrayresult[imagescount-1],mysheet->name);
				else
					printf("Xray Technician [%d] records [%s] on Child Patient [%d]'s examination sheet.\n",which,mysheet->xrayresult[imagescount-1],mysheet->name);

	}
	
	if(testcontrol==7)
		if(mysheet->patientType==1)
			printf("X-ray Technician [%d] tells [Adult Patient] [%d] to wait in Xray waiting room.\n",which,mysheet->name);
		else
			printf("X-ray Technician [%d] tells [Parent] [%d] to wait in Xray waiting room.\n",which,mysheet->name);
/*
	xrayOutLock.Acquire();
	xrayOutQueue->Append((void*)mysheet);		//put the sheet in the wall pocket	
	xrayCondition[which]->Signal(xrayLock[which]);
	xrayOutLock.Release();
*/	
	if(testcontrol==7)
		if(mysheet->patientType==1)
			printf("X-ray Technician [%d] puts [Adult Patient] [%d] in Xray waiting room wall pocket.\n",which,mysheet->name);
		else
			printf("X-ray Technician [%d] puts [Parent] [%d] in Xray waiting room wall pocket.\n",which,mysheet->name);
	
	xrayLock[which]->Release();
	
	}

}

